package cn.tedu.car.backend.customer.dao.repository.impl;

import cn.tedu.car.backend.base.response.PageData;
import cn.tedu.car.backend.customer.dao.mapper.CustomerMapper;
import cn.tedu.car.backend.customer.dao.repository.CustomerRepository;
import cn.tedu.car.backend.customer.pojo.dto.CustomerQueryParam;
import cn.tedu.car.backend.customer.pojo.entity.Customer;
import cn.tedu.car.backend.customer.pojo.vo.CustomerDetailVo;
import cn.tedu.car.backend.customer.pojo.vo.CustomerListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author LLY
 */
@Repository
@Slf4j
public class CustomerRepositoryImpl implements CustomerRepository {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageData list(CustomerQueryParam param) {
        log.debug("正在执行【获取客户列表】的数据访问");
        List<CustomerListVo> list = customerMapper.list(param);
        PageData pageData = new PageData();
        //向PageData对象中装存入总记录数
        pageData.setList(list);
        pageData.setTotal(customerMapper.selectCustomerCount(param));
        return pageData;
    }

    @Override
    public Customer getCustomerByUsername(String username) {
        log.debug("正在执行【根据用户名获取客户信息】的数据访问，入参{}", username);
        return customerMapper.getCustomerByUsername(username);
    }

    @Override
    public void insert(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public Customer getCustomerByPhone(String phone) {
        return null;
    }

    @Override
    public Customer getCustomerById(Long customerId) {
        log.debug("正在执行【根据id获取客户信息】的数据访问，入参{}", customerId);
        return customerMapper.getCustomerById(customerId);
    }

    @Override
    public void update(Customer customer) {
        log.debug("正在执行【更新客户信息】的数据访问，入参{}", customer);
        customerMapper.update(customer);
    }

    @Override
    public CustomerDetailVo getCustomerDetailById(Long id) {
        CustomerDetailVo customerDetailById = customerMapper.getCustomerDetailById(id);
        if (customerDetailById.getIsIdentity() != 1){
            customerDetailById.setPersonNum("");
            customerDetailById.setName("");
        }
        return customerDetailById;
    }
}
